﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class UserAnswerDAO
    {
        private TestOnlineDBEntities db = new TestOnlineDBEntities();

        public int Insert(List<UserAnswer> userAnswers)
        {
            try
            {
                // User selected answers.
                if (userAnswers.Count > 0)
                {
                    var userTestId = userAnswers[0].UserTestId;
                    // Delete old answers.
                    db.UserAnswers.Where(a => a.UserTestId == userTestId).ToList().ForEach(db.DeleteObject);


                    // Insert new answers.
                    foreach (var u in userAnswers)
                    {
                        db.UserAnswers.AddObject(u);
                    }

                    int result = db.SaveChanges();
                    return result;
                }
                // User don't select any answer.
                else
                {
                    return 1;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public UserAnswer SelectByUserTestIdQuestionIdAnswerId(int userTestId, int questionId, int answerId)
        {
            try
            {
                var userAnswer = db.UserAnswers.Where(a => (a.QuestionId == questionId
                    && a.UserTestId == userTestId
                    && a.AnswerId == answerId)).FirstOrDefault();
                return userAnswer;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<UserAnswer> SelectByUserTestIdQuestionId(int userTestId, int questionId)
        {
            try
            {
                var userAnswers = db.UserAnswers.Where(a => (a.QuestionId == questionId
                    && a.UserTestId == userTestId)).ToList();
                return userAnswers;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<UserAnswer> SelectByUserTestId(int userTestId)
        {
            try
            {
                var userAnswers = db.UserAnswers.Where(a => (a.UserTestId == userTestId)).ToList();
                return userAnswers;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<UserAnswer> SelectByQuestionIdAnswerId(int questionId, int answerId)
        {
            try
            {
                var userAnswers = db.UserAnswers.Where(u => (u.QuestionId == questionId && u.AnswerId == answerId)).ToList();
                return userAnswers;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<UserAnswer> SelectByQuestionId(int id)
        {
            try
            {
                var userAnswers = db.UserAnswers.Where(u => u.QuestionId == id).ToList();
                return userAnswers;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<UserAnswer> SelectByQuestionIdMatchId(int questionId, int matchId)
        {
            try
            {
                var userAnswers = db.UserAnswers.Where(u => (u.QuestionId == questionId
                    && u.MatchId == matchId)).ToList();
                return userAnswers;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<UserAnswer> SelectByQuestionIdAnswerIdMatchId(int questionId, int answerId, int matchId)
        {
            try
            {
                var userAnswers = db.UserAnswers.Where(u => (u.AnswerId == answerId
                    && u.MatchId == matchId && u.QuestionId == questionId)).ToList();
                return userAnswers;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Insert(List<UserAnswer> userAnswers, string type)
        {
            try
            {
                if (type.Equals("QuickTest"))
                {
                    if (userAnswers.Count > 0)
                    {
                        foreach (var u in userAnswers)
                        {
                            db.UserAnswers.AddObject(u);
                        }
                        int result = db.SaveChanges();
                        return result;
                    }
                    // User don't select any answer.
                    return 1;
                }
                return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Insert new Answer of User
        /// </summary>
        /// <param name="userAnswer">UserAnswer object</param>
        /// <returns>Number of UserAnswer has been inserted</returns>

        public int Insert(UserAnswer userAnswer)
        {
            try
            {
                db.UserAnswers.AddObject(userAnswer);
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}